package service;

import models.IdSec;

import org.springframework.stereotype.Service;

import utils.NumberUtils;

@Service
public class IdSecService extends BaseManager{
	
	public String getNextCode(String prefix,int maxLen){
		IdSec idSec = (IdSec)getDao().findFirst("from IdSec where prefix=?", prefix);
		if(idSec==null){
			idSec = new IdSec();
			idSec.setPrefix(prefix);
			idSec.setSec(0);
			getDao().save(idSec);
		}
		
		int sec = idSec.getSec();
		int nextSec = sec + 1;
		String nextSecString= String.valueOf(nextSec);
		String returnString = NumberUtils.formatWithZeroPrefix(nextSecString, maxLen);
		
		getDao().executeUpdate("update IdSec set sec=? where prefix=?", nextSec,prefix);
		
		return prefix + returnString;
	}
}
